package exp;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import tools.FileWriterEnhance;
import tools.ObjectTools;

/**
 * Output pure texts for the CNN/USATODAY highlights and all titles for all html pages.
 * 
 * @author Lanjun
 */
public class EXP6 {
	
	/**
	 * 
	 * @param inputPath INPUT
	 * @param outputPath OUTPUT
	 * @param outputPathSelect OUTPUT for only CNN/USATODAY title and highlights
	 */
	public void run(String inputPath, String outputPath, String outputPathSelect) {
		for (File file : (new File(inputPath)).listFiles()) {
			@SuppressWarnings("unchecked")
			HashMap<String, URLContent> hl = (HashMap<String, URLContent>) ObjectTools.readFromFile(file);

			HashMap<String, Boolean> titleList = new HashMap<>();
			StringBuilder sb = new StringBuilder();
			StringBuilder sb1 = new StringBuilder();

			for (URLContent hc : hl.values()) {
				if (titleList.containsKey(hc.title) || hc.title == null) {
					continue;
				} else {
					URL url = null;
					try {
						url = new URL(hc.url);
					} catch (MalformedURLException e) {
						e.printStackTrace();
					}
					String host = url.getHost();
					if (hc.highlights != null) {
						if (host.equals("edition.cnn.com") && (!hc.url.startsWith("http://edition.cnn.com/video"))
								&& (!hc.url.startsWith("http://edition.cnn.com/SPECIALS"))
								&& (!hc.url.startsWith("http://edition.cnn.com/interactive"))) {
							sb1.append(hc.url);
							sb1.append('\n');
							sb1.append(hc.title);
							sb1.append('\n');
							for (String highlights : hc.highlights) {
								sb1.append(highlights);
								sb1.append("\t,,\t");
							}
							sb1.append('\n');
							sb1.append('\n');
						}  else if (host.equals("www.usatoday.com")
								&& (!hc.url.startsWith("http://www.usatoday.com/videos"))) {
							sb1.append(hc.url);
							sb1.append('\n');
							sb1.append(hc.title);
							sb1.append('\n');
							for (String highlights : hc.highlights) {
								sb1.append(highlights);
								sb1.append("\t,,\t");
							}
							sb1.append('\n');
							sb1.append('\n');
						}
					}
					titleList.put(hc.title, true);
					sb.append(hc.url);
					sb.append('\n');
					sb.append(hc.title);
					sb.append('\n');
					if (hc.highlights != null) {
						for (String highlights : hc.highlights) {
							sb.append(highlights);
							sb.append("\t,,\t");
						}
					}
					sb.append('\n');
					sb.append('\n');
				}
			}

			FileWriterEnhance fwe = new FileWriterEnhance(outputPath + file.getName(), "UTF-8");
			FileWriterEnhance fweCNN = new FileWriterEnhance(outputPathSelect + file.getName(), "UTF-8");
			fwe.WriteToFile(sb.toString());
			fweCNN.WriteToFile(sb1.toString());
		}
	}
	
	public static void main(String[] args) {
		String inputPath = "data/_newData/obj/highlights/";
		String outputPath = "data/_newData/highlights/";
		String outputPathSelect = "data/_newData/highlights_CNN_USATODAY/";
		
		EXP6 exp6 = new EXP6();
		exp6.run(inputPath, outputPath, outputPathSelect);
	}
}
